home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / NetPokerForMacOSX_Server / HoldEmHigh / PokerTable.m < prev    next >
Encoding:
Text File  |  1999-06-26  |  14.2 KB  |  506 lines

  1. // PokerTable.m
  2.  
  3. #import "poker.h"
  4. #import "IRCTask.h"
  5.  
  6. #define USER ([self findPlayerWithName:[[[NSApp delegate] userName]cString]])
  7. @implementation PokerTable
  8.  
  9. // -----------------------------------------------------------------------------
  10. // Override returning the nib file name of the document
  11.  
  12. - (NSString *)windowNibName {
  13.     return @"PokerTable";
  14. }
  15.  
  16. // -----------------------------------------------------------------------------
  17. // Override
  18.  
  19. - (void)windowControllerDidLoadNib:(NSWindowController *) aController{
  20.     [super windowControllerDidLoadNib:aController];
  21.     [self newPokerTable];
  22. }
  23.  
  24. // -----------------------------------------------------------------------------
  25. // Override
  26.  
  27. - (NSData *)dataRepresentationOfType:(NSString *)aType {
  28.     return nil;
  29. }
  30.  
  31. // -----------------------------------------------------------------------------
  32. // Override
  33.  
  34. - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
  35.     return YES;
  36. }
  37.  
  38. - (IBAction)publicMessagePlayerAction:(id)sender { // send message to all
  39.     [[Messager sharedInstance] showWindow:self];
  40. }
  41.  
  42. - (IBAction)privateMessagePlayerAction:(id)sender { // click on a player to send message
  43.     NSString *toWhom = [(id)[sender selectedTag] playerName];
  44.     if (![[[NSApp delegate] userName] isEqualToString:toWhom]) {
  45.         [[Messager messagerWithRecipient:toWhom] showWindow:self];
  46.     }
  47. }
  48.  
  49.  
  50. - (IBAction)foldAction:(id)sender {
  51.     [[IRCTask sharedIRCTask] fold];
  52.  
  53. }
  54.  
  55. - (IBAction)callAction:(id)sender {
  56.     [[IRCTask sharedIRCTask] call];
  57. }
  58.  
  59.  
  60. - (IBAction)raisePotAction:(id)sender {
  61.     [[IRCTask sharedIRCTask] pot];
  62. }
  63.  
  64.  
  65. - (IBAction)raiseHalfPotAction:(id)sender {
  66.     [[IRCTask sharedIRCTask] halfPot];
  67.  
  68. }
  69.  
  70.  
  71. - (IBAction)raiseMaximumAction:(id)sender {
  72.     [[IRCTask sharedIRCTask] pushAllIn];
  73.  
  74. }
  75.  
  76.  
  77. - (IBAction)raiseMinimumAction:(id)sender {
  78.     [[IRCTask sharedIRCTask] minBet];
  79.  
  80. }
  81.  
  82.  
  83. - (IBAction)raiseSpecifiedAmountAction:(id)sender {
  84.     [[IRCTask sharedIRCTask] bet:[raiseSpecifiedField intValue]];
  85.  
  86. }
  87.  
  88. - (void)clearCell:(NSButtonCell *)cell {
  89.     [cell setTag:0];
  90.     [cell setBordered:NO];
  91.     [cell setTransparent:YES];
  92.     [cell setTitle:@""];
  93.     [cell setEnabled:NO];
  94.     [playersMatrix drawCell:cell];
  95. }
  96.  
  97. - (void)populateCell:(NSButtonCell *)cell withPlayer:(Player *)player {
  98.     [cell setTag:(int)player];
  99.     [cell setBordered:YES];
  100.     [cell setTransparent:NO];
  101.     [cell setTitle:[player buttonTitle]];
  102.     [cell setEnabled:YES];
  103.     [playersMatrix drawCell:cell];
  104. }
  105.  
  106.  
  107. - (void)emptyPlayers {
  108.     NSArray *cells = [playersMatrix cells];
  109.     unsigned i = [cells count];
  110.     while (i-- > 0) [self clearCell:[[playersMatrix cells] objectAtIndex:i]];
  111. }
  112.  
  113.  
  114. - (void)updateCardCells {
  115.    unsigned i = 5;
  116.    while (i--) {
  117.        NSButtonCell *cell = [publicCardsMatrix cellAtRow:0 column:i];
  118.        [cell setImage:[[[hand cards] objectAtIndex:i] image]];
  119. //       [cell setImageDimsWhenDisabled:NO];
  120. //       [cell setEnabled:NO];
  121.    }
  122.    [publicCardsMatrix display];
  123. }
  124.  
  125. - (void)drawHoleCards {
  126.     Player *user = USER;
  127.     if (!user) {
  128.         [[holeCardsMatrix cellAtRow:0 column:0] setImage:[NSImage imageNamed:@"0"]];
  129.         [[holeCardsMatrix cellAtRow:0 column:1] setImage:[NSImage imageNamed:@"0"]];
  130.     } else {
  131.         [[holeCardsMatrix cellAtRow:0 column:0] setImage:[[[user holeHand]firstHoleCard] image]];
  132.         [[holeCardsMatrix cellAtRow:0 column:1] setImage:[[[user holeHand]secondHoleCard] image]];
  133.     }
  134. /*
  135.     [[holeCardsMatrix cellAtRow:0 column:0] setImageDimsWhenDisabled:NO];
  136.     [[holeCardsMatrix cellAtRow:0 column:0] setEnabled:NO];
  137.     [[holeCardsMatrix cellAtRow:0 column:1] setImageDimsWhenDisabled:NO];
  138.     [[holeCardsMatrix cellAtRow:0 column:1] setEnabled:NO];
  139. */
  140.     [holeCardsMatrix display];
  141. }
  142.  
  143. - (void)newHand {
  144.     buttonPlayer = nil;
  145.     //user = nil;
  146.     currentPlayer = nil;
  147.     //[players makeObjectsPerformSelector:@selector(getNewHand)];
  148.     [hand release];
  149.     hand = [[HoldEmHighHand allocWithZone:[self zone]] init];
  150.     [self updateCardCells];
  151.     [self drawHoleCards];
  152.     [self readyToBeginGame];
  153. }
  154.  
  155. // hooks for cliff
  156. - (void)newPokerTable {
  157.        // clear out all players
  158.        // turn over the main 5 cards and 2 hole cards
  159.    players = [[NSMutableArray alloc] init];
  160.    [self emptyPlayers];
  161.  //  [self newHand];
  162. }
  163.  
  164. - (void)userGotFirstHoleCard:(Card *)card secondCard:(Card *)secondCard {
  165.     Player *user = USER;
  166.     [[user holeHand] setSecondHoleCard:secondCard];
  167.     [[user holeHand] setFirstHoleCard:card];
  168.     [self drawHoleCards];
  169. }
  170.  
  171.  
  172. // pass in 0-4 for index
  173. // and a 
  174. - (void)publicCardNumber:(int)indexOfCard didShowCard:(Card *)card {
  175.     NSButtonCell *cell = [publicCardsMatrix cellAtRow:0 column:indexOfCard];
  176.     [hand setCard:card atIndex:indexOfCard];
  177.     [cell setImage:[card image]];
  178.     [publicCardsMatrix drawCellAtRow:0 column:indexOfCard];
  179. }
  180.  
  181. typedef struct {
  182.     int row;
  183.     int col;
  184. } RowCol;
  185.  
  186. static RowCol rowcols[24] = {
  187.      { 0, 0 },
  188.      { 0, 1 },
  189.      { 0, 2 },
  190.      { 0, 3 },
  191.      { 0, 4 },
  192.      { 0, 5 },
  193.      { 0, 6 },
  194.      { 1, 6 },
  195.      { 2, 6 },
  196.      { 3, 6 },
  197.      { 4, 6 },
  198.      { 5, 6 },
  199.      { 6, 6 },
  200.      { 6, 5 },
  201.      { 6, 4 },
  202.      { 6, 3 },  /* THIS IS WHERE I AM - 16th item */
  203.      { 6, 2 },
  204.      { 6, 1 },
  205.      { 6, 0 },
  206.      { 5, 0 },
  207.      { 4, 0 },
  208.      { 3, 0 },
  209.      { 2, 0 },
  210.      { 1, 0 }
  211. };
  212.  
  213. #define ME_INDEX  15
  214.  
  215. - (void)readyToBeginGame {
  216.     // what is the index of me?
  217.     // how many players are there?
  218.     // distribute them around by assigning them row's and col's
  219.     int i, total = [players count];
  220.     float skip = MAX(1,(int)(NUM_POSSIBLE_PLAYER_CELLS/(float)total));
  221.     int indexOfMe = [players indexOfObject:USER];
  222.     int currIndex;
  223.     int baseIndex;
  224.     currIndex = ME_INDEX - rint(indexOfMe * skip);
  225.     if (indexOfMe == NSNotFound) currIndex = 0;
  226.  
  227.     if (currIndex < 0 ) currIndex += NUM_POSSIBLE_PLAYER_CELLS;
  228.     baseIndex = currIndex;
  229.  
  230.     [self emptyPlayers];  // clears all cells
  231.     for (i = 0; i < total; i++) {
  232.          Player *player = [players objectAtIndex:i];
  233.          [player setRow:rowcols[currIndex].row];
  234.          [player setColumn:rowcols[currIndex].col];
  235.          [self populateCell:[playersMatrix cellAtRow:[player row] column:[player column]]
  236.         withPlayer:player];
  237.      currIndex += skip;
  238.          if (currIndex >= NUM_POSSIBLE_PLAYER_CELLS) currIndex -= NUM_POSSIBLE_PLAYER_CELLS;
  239.     }
  240.     
  241. }
  242.  
  243. - (void)updatePlayerTitle:(Player *)player {
  244.     NSButtonCell *cell = [playersMatrix cellAtRow:[player row] column:[player column]];
  245.     //[cell setEnabled:[player hasFolded]];
  246.     [cell setTitle:[player buttonTitle]];
  247.     [playersMatrix drawCellAtRow:[player row] column:[player column]];
  248. }
  249.  
  250. - (void)player:(const char *)player hasCash:(int)amount {
  251.     Player *play = [self findPlayerWithName:player];
  252.     if (play) {
  253.         if ([play isMe]) [myPot updatePotAmount:[play stackAmount]];
  254.  
  255.         [play setStackAmount:amount];
  256.         [self updatePlayerTitle:play];
  257.     }
  258. }
  259.  
  260. - (void)beginAddingPlayers {
  261.     buttonPlayer = nil;
  262.     currentPlayer = nil;
  263.     hasInitedPlayers = NO;
  264.     [players removeAllObjects];
  265. }
  266.  
  267.  
  268. - (void)addPlayer:(const char *)player cash:(int)amount{
  269.    // find a free cell
  270.    // todo: good heuristic to assign players around table evenly
  271.    Player *newPlayer;
  272.    if ((newPlayer = [self findPlayerWithName:player]) == nil) {
  273.     newPlayer = [[Player allocWithZone:[self zone]] initWithName:[NSString stringWithCString:player] pokerTable:self];
  274.     [players addObject:newPlayer];
  275.     }
  276.    [newPlayer setStackAmount:amount];
  277. }
  278.  
  279.  
  280. - (void)deletePlayer:(const char *)player  {
  281.     // find the cell of the player, remove it, redraw it
  282.     // remove the player from list
  283.     Player *remove = [self findPlayerWithName:player];
  284.     if (remove) {
  285.         [self clearCell:[playersMatrix cellWithTag:(int)remove]];
  286.     [players removeObject:remove];
  287.     }
  288. }
  289.  
  290.  
  291.  
  292. - (Player *)findPlayerWithName:(const char *)playerName {
  293.     if (!playerName) return nil;
  294.     else {
  295.         NSString *pName = [NSString stringWithCString:playerName];
  296.         unsigned i = [players count];
  297.         while (i-- > 0) {
  298.         if ([[[players objectAtIndex:i] playerName] isEqualToString:pName])
  299.                 return [players objectAtIndex:i];
  300.         }
  301.         return nil;
  302.     }
  303. }
  304.  
  305.  
  306. - (void)setButtonPlayer:(const char *)playerName {
  307.     Player *oldButton = buttonPlayer;
  308.     buttonPlayer = [self findPlayerWithName:playerName];
  309.     if (oldButton)
  310.     [self populateCell:[playersMatrix cellWithTag:(int)oldButton] withPlayer:oldButton];
  311.     if (buttonPlayer)
  312.         [self populateCell:[playersMatrix cellWithTag:(int)buttonPlayer] withPlayer:buttonPlayer];
  313.     // PENDING draw focus ring
  314. }
  315.  
  316.  
  317. - (BOOL)isButtonPlayer:(Player *)player {
  318.     if (!buttonPlayer) return NO;
  319.     return [[player playerName] isEqualToString:[buttonPlayer playerName]];
  320. }
  321.  
  322. // NSMatrix:
  323. // NSButtonCell - _drawKeyboardFocusRingWithFrame:frame inView:controlView
  324. /*
  325. NSKeyboardFocusRingPrivate
  326. _NSKeyboardFocusClipView
  327. {?="highlightMode"b1"radioMode"b1"listMode"b1"allowEmptySel"b1"autoscroll"b1"selectionByRect"b1"drawsCellBackground"b1"drawsBackground"b1"autosizeCells"b1"drawingAncestor"b1"tabKeyTraversesCells"b1"tabKeyTraversesCellsExplicitlySet"b1"allowsIncrementalSearching"b1"currentlySelectingCell"b1"onlySetKeyCell"b1"changingSelectionWithKeyboard"b1"dontScroll"b1"refusesFirstResponder"b1"useSimpleTrackingMode"b1"checkForSimpleTrackingMode"b1"reservedMatrix"b12}
  328. _changingSelectionWithKeyboard
  329. _drawKeyboardFocusRingWithFrame:inView:
  330. _setKeyboardFocusRingNeedsDisplay
  331. _drawKeyboardFocusRingWithFrame:
  332. */
  333.  
  334. - (void)setPlayersTurn:(const char *)playerName {
  335.     NSButtonCell *cell;
  336.     NSRect r;
  337.  
  338.     currentPlayer = [self findPlayerWithName:playerName];
  339.     cell = [playersMatrix cellWithTag:(int)currentPlayer];
  340.     r = [playersMatrix cellFrameAtRow:[currentPlayer row] column:[currentPlayer column]];
  341.     r = NSInsetRect(r, -4.0, -4.0);
  342.     if (oldCell){
  343.         [oldCell _setKeyboardFocusRingNeedsDisplay];
  344.     [[[playersMatrix window] contentView] setNeedsDisplayInRect:[[[playersMatrix window] contentView]bounds]];
  345.         [[playersMatrix window] display];
  346.  
  347.    }
  348.     [cell _drawKeyboardFocusRingWithFrame:r inView:playersMatrix];
  349.     oldCell = cell;
  350. oldRect = [playersMatrix convertRect:oldRect toView:[[playersMatrix window] contentView]];
  351.     oldRect = NSInsetRect(oldRect,-12.0,-12.0);
  352.     if ([[currentPlayer playerName] isEqualToString:[[NSApp delegate] userName]])
  353.         [[NSSound soundNamed:@"OpenShutter"] play];
  354. }
  355.  
  356. - (void)setStatusString:(NSString *)message {
  357.     [statusField setStringValue:message];
  358. }
  359.  
  360. - (HoldEmHighHand *) hand
  361. {
  362.   return hand;
  363. }
  364.  
  365. - (void)playerQuits:(const char *) playerName n_left:(int)nleft
  366. {
  367.    Player *player = [self findPlayerWithName:playerName];
  368.     if (player) {
  369.     [players removeObject:player];
  370.         [self readyToBeginGame];
  371.     }
  372. }
  373.  
  374. - (void)tourneyStarted
  375. {
  376.  //   [self newHand];
  377.     [self emptyPlayers];  // clears all cells
  378.  
  379. }
  380.  
  381. - (void)blindsAre:(int) small_blind and:(int) big_blind
  382. {
  383.     
  384. }
  385.  
  386. - (void)player:(const char *)playerName toCall:(int)to_call
  387. {
  388.     [self setPlayersTurn:playerName];
  389.     // state this PENDING
  390. }
  391.  
  392. - (void)decrementAmount:(int)amount fromPlayer:(Player *)player {
  393.     [player setStackAmount:[player stackAmount]- amount];
  394.     [self updatePlayerTitle:player];
  395.     if ([player  isMe]) [myPot updatePotAmount:[player stackAmount]];
  396. }
  397.  
  398. - (void)incrementAmount:(int)amount toPlayer:(Player *)player {
  399.     [player setStackAmount:[player stackAmount] + amount];
  400.     [self updatePlayerTitle:player];
  401.     if ([player  isMe]) [myPot updatePotAmount:[player stackAmount]];
  402. }
  403.  
  404. - (void)player:(const char *)playerName blinds:(int)blind potNow:(int)pot allIn:(BOOL)allIn
  405. {
  406.     if (!hasInitedPlayers) {
  407.          [self newHand];
  408.          hasInitedPlayers = YES;
  409.     }
  410.     {
  411.     Player *player = [self findPlayerWithName:playerName];
  412.     [thePot updatePotAmount:pot];
  413.     [self decrementAmount:blind fromPlayer:player];
  414.     [myPot updatePotAmount:[USER stackAmount]];
  415.     }
  416. }
  417.  
  418. - (void) blindsWillDoubleIn:(int)blind_remaining units:(const char *)blind_remaining_units
  419. {
  420. // PENDING - place 10 second status string
  421. }
  422.  
  423. - (void) beginHand:(int)hand_no n_players:(int)n_players
  424. {
  425.      [self newHand];
  426. }
  427.  
  428. - (void)player:(const char *)playerName messages:(const char *)message {
  429.     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithCString:message], @"Message",nil];
  430.     [[NSNotificationCenter defaultCenter] postNotificationName:MessageReceivedNotification object:[NSString stringWithCString:playerName] userInfo:dict];
  431. }
  432.  
  433.  
  434. - (void) player:(const char *)playerName foldsLeaving:(int)n_players
  435. {
  436.     Player *player = [self findPlayerWithName:playerName];
  437.     [player fold];
  438.     [self updatePlayerTitle:player];
  439. }
  440.  
  441. - (void) player:(const char *)playerName calls:(int)amount potNow:(int)pot allIn:(BOOL)allIn
  442. {
  443.     Player *player = [self findPlayerWithName:playerName];
  444.     [thePot updatePotAmount:pot];
  445.     [self decrementAmount:amount fromPlayer:player];
  446. }
  447.  
  448. - (void) player:(const char *)playerName vacationedBy:(const char *)by_whom
  449. {
  450.     Player *player = [self findPlayerWithName:playerName];
  451.     [player setOnVacation:YES];
  452.     [self updatePlayerTitle:player];
  453. }
  454.  
  455. - (void) playerChecks:(const char *)player
  456. {
  457. }
  458.  
  459. - (void) player:(const char *)playerName bets:(int)amount potNow:(int)pot allIn:(BOOL)allIn
  460. {
  461.     Player *player = [self findPlayerWithName:playerName];
  462.     [thePot updatePotAmount:pot];
  463.     [self decrementAmount:amount fromPlayer:player];
  464. }
  465.  
  466. - (void) playerBackFromVacation:(const char *)playerName
  467. {
  468.     Player *player = [self findPlayerWithName:playerName];
  469.     [player setOnVacation:NO];
  470.     [self updatePlayerTitle:player];
  471. }
  472.  
  473. - (void) player:(const char *)playerName wins:(int) amount withHand:(const char *)hand
  474. {
  475.     Player *player = [self findPlayerWithName:playerName];
  476.     [thePot updatePotAmount:[thePot pot] - amount];
  477.     [self incrementAmount:amount toPlayer:player];
  478. }
  479.  
  480. - (void) aboutToShowHands
  481. {
  482. }
  483.  
  484. - (void) player:(const char *)playerName raises:(int)amount potNow:(int)pot allIn:(BOOL)allIn
  485. {
  486.     Player *player = [self findPlayerWithName:playerName];
  487.     int oldPot = [thePot pot];
  488.     [thePot updatePotAmount:pot];
  489.     [self decrementAmount:pot - oldPot fromPlayer:player];
  490. }
  491.  
  492. - (void) playerBusted:(const char *)playerName
  493. {
  494.     Player *player = [self findPlayerWithName:playerName];
  495.     [self decrementAmount:[player stackAmount] fromPlayer:player];
  496. }
  497.  
  498. - (void) playerQuit:(const char *)playerName
  499. {
  500. // stick up a tombstone PENDING
  501.     [self deletePlayer:playerName];
  502.  
  503. }
  504.  
  505. @end
  506.